﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ComponentModel;
using MongoDB.Bson;
using MongoDB.Driver;
using Ahi.Entity.Interfaces;

namespace Ahi.Entity
{
    public class BaseEntity<DerivedType> : IEntityBase
        where DerivedType : class,new()
    {
        public BaseEntity() { }

        public ObjectId Id { get; set; }

        [Browsable(false)]
        public bool Passive { get; private set; }

        public void MarkAsDeleted()
        {
            Passive = true;
        }

        public string EntityType
        {
            get
            {
                return Utilities.MongoUtil.GetDBCollectionName(typeof(DerivedType));
            }
        }

        public MongoCollection<DerivedType> Collection()
        {
            return MongoAdaptor.DB.GetCollection<DerivedType>(EntityType);
        }

        public void Delete()
        {
            Passive = true;
            MongoAdaptor.DB.GetCollection<DerivedType>(EntityType).Save(this);
        }

        public bool Save()
        {
            SafeModeResult result = Collection().Save(this);
            if (result == null) return false;
            return result.Ok;
        }
    }
}
